﻿using System.Data;

namespace WebCoreEmptyToMiddleWare
{
    public class Linq基本使用
    {
        Dictionary<string, int> students = new Dictionary<string, int>
                {
                    { "Alice", 85 },
                    { "Bob", 92 },
                    { "Charlie", 78 },
                    { "David", 90 },
                    { "Eve", 88 }
                };

        List<IncomeInfo> incomeList = new List<IncomeInfo>()
              {
                 new IncomeInfo(){IncomeId=1,IncomeName="收入1",ItemId=101},
                 new IncomeInfo(){IncomeId=2,IncomeName="收入2",ItemId=101},
                 new IncomeInfo(){IncomeId=3,IncomeName="收入3",ItemId=103},
                 new IncomeInfo(){IncomeId=4,IncomeName="收入4",ItemId=102},
                 new IncomeInfo(){IncomeId=5,IncomeName="收入5",ItemId=103},
                 new IncomeInfo(){IncomeId=6,IncomeName="收入6",ItemId=104},
                 new IncomeInfo(){IncomeId=7,IncomeName="收入7",ItemId=102},
                 new IncomeInfo(){IncomeId=8,IncomeName="收入8",ItemId=105},
                 new IncomeInfo(){IncomeId=9,IncomeName="收入9",ItemId=104},
                 new IncomeInfo(){IncomeId=10,IncomeName="收入10",ItemId=106}
              };


        public void BaseUses()
        {
            var result = from st in students  //懒加载
                         where st.Value > 85
                         orderby st.Value descending
                         //select st;
                         select new { Name = st.Key, Count = st.Value };  //返回一个匿名类型
            foreach (var student in result) //大于85的
            {
                //Console.WriteLine($"姓名: {student.Key}, 分数: {student.Value}");
                Console.WriteLine($"姓名: {student.Name}, 分数: {student.Count}");
            }

            var list1 = from list in incomeList
                        group list by list.ItemId;  // group list是进行分组后取值，  by 是对哪个分组
                                                    //into incomesNew
                                                    //from newIncome in incomesNew.DefaultIfEmpty()  //如果为空即给默认值
                                                    //select newIncome;
            foreach (var groupList in list1)
            {
                Console.WriteLine(groupList.Key);
                foreach (var item in groupList)
                {
                    Console.WriteLine(item.IncomeId + "--" + item.IncomeName);
                }
            }


        }
    }



    public class IncomeInfo
    {
        public int IncomeId { get; set; }
        public string IncomeName { get; set; }
        public int ItemId { get; set; }


    }

}
